import numpy as np


class CheeseBoard():
    '''
    定义棋盘大小:   15*15
    定义棋子：      1先行 2后行 空缺位置补0
    定义落子位置:   1<=x, y<=15
    定义坐标：      标准二维平面直角坐标系，左下角原点
    '''
    def __init__(self):
        self.board=np.zeros(shape=(16, 16),dtype=int)
        
    def put(self, y:int, x:int, color:1|2)->bool:
        '''
        :input x: 落子位置x坐标
        :input y: 落子位置y坐标
        :input color: 落子颜色
        
        :output:  返回落子是否成功
        '''
        assert x>=1 and x<=15, "x>=1 and x<=15"
        assert y>=1 and y<=15, "y>=1 and y<=15"
        assert color==1 or color==2, "color==1 or color==2"
        
        if self.board[y][x]!=0:
            # LOG:这里可以添加日志
            return False
        self.board[y][x]=color
        return True
        
    def putable(self, y:int, x:int)->bool:
        assert x>=1 and x<=15, f"x>=1 and x<=15 x={x} y={y}"
        assert y>=1 and y<=15, "y>=1 and y<=15"
        
        if self.board[y][x]!=0:
            # LOG:这里可以添加日志
            return False
        return True
        
    def get_putted(self):
        res=[]
        for y in range(1,16):
            for x in range(1,16):
                if self.board[y][x]!=0:
                    if self.board[y][x]==1:
                        res.append([[y,x],1])
                    else:
                        res.append([[y,x],2])
        return res
        
    def check_win(self):#判断五子连心
        for i in range(1, 16):
            pos1=[]
            pos2=[]
            for j in range(1, 16):
                if self.board[i][j]==1:
                    pos1.append([i,j])
                else:
                    pos1=[]
                if self.board[i][j]==2:
                    pos2.append([i,j])
                else:
                    pos2=[]
                if len(pos1)>=5:#五子连心
                    return [1,pos1]
                if len(pos2)>=5:
                    return [2,pos2]

        for j in range(1,16):
            pos1=[]
            pos2=[]
            for i in range(1, 16):
                if self.board[i][j]==1:
                    pos1.append([i,j])
                else:
                    pos1=[]
                if self.board[i][j]==2:
                    pos2.append([i,j])
                else:
                    pos2=[]
                if len(pos1)>=5:
                    return [1,pos1]
                if len(pos2)>=5:
                    return [2,pos2]
        for i in range(1, 16):
            for j in range(1, 16):
                pos1=[]
                pos2=[]
                for k in range(1, 16):
                    if i+k>=16 or j+k>=16:
                        break
                    if self.board[i+k][j+k]==1:
                        pos1.append([i+k,j+k])
                    else:
                        pos1=[]
                    if self.board[i+k][j+k]==2:
                        pos2.append([i+k,j+k])
                    else:
                        pos2=[]
                    if len(pos1)>=5:
                        return [1,pos1]
                    if len(pos2)>=5:
                        return [2,pos2]
        for i in range(1, 16):
            for j in range(1, 16):
                pos1=[]
                pos2=[]
                for k in range(1, 16):
                    if i+k>=16 or j-k<=0:
                        break
                    if self.board[i+k][j-k]==1:
                        pos1.append([i+k,j-k])
                    else:
                        pos1=[]
                    if self.board[i+k][j-k]==2:
                        pos2.append([i+k,j-k])
                    else:
                        pos2=[]
                    if len(pos1)>=5:
                        return [1,pos1]
                    if len(pos2)>=5:
                        return [2,pos2]
        return [0,[]]
